package jzq.spring.bean;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * @author jzq
 * @date 2019/3/12 10:14
 */
public class SpringIocApp {


    @Test
    public void beanScopeTest() {
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        // Singleton是单例类型，在创建容器时就同时自动创建了一个bean的对象 spring默认采用singleton作用域
        SingletonBean singletonBean1 = (SingletonBean) context.getBean("singletonBean");
        SingletonBean singletonBean2 = (SingletonBean) context.getBean("singletonBean");
        System.out.println("singletonBean1.equals(singletonBean2) is : " + singletonBean1.equals(singletonBean2));

        // prototype 作用域
        PrototypeBean protoBean1 = (PrototypeBean) context.getBean("prototypeBean");
        protoBean1.setMessage("I'm prototype bean");
        PrototypeBean protoBean2 = (PrototypeBean) context.getBean("prototypeBean");
        protoBean2.getMessage();
        //  prototype类型的bean它在我们创建容器的时候并没有实例化而是当我们获取bean的时候才会去创建一个对象，
        // 而且我们每次获取到的对象都不是同一个对象"
        System.out.println("protoBean1.equals(protoBean2) is : " + protoBean1.equals(protoBean2));

    }

    @Test
    public void lifeCycleTest() {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        context.getBean("exampleBean");
        // 销毁
        context.registerShutdownHook();
    }
}
